// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

package edit
{
    import common.MoveThumbComponent;

    import flash.display.BitmapData;
    import flash.display.MovieClip;
    import flash.geom.Matrix;
    import flash.geom.Rectangle;

    public class EditViewport
    {
        private var mGUIMc:MovieClip;
        private var mEditAreaMc:MovieClip;

        private var mHorizontalLeftThumb:MoveThumbComponent;
        private var mHorizontalRightThumb:MoveThumbComponent;
        private var mVerticalTopThumb:MoveThumbComponent;
        private var mVerticalBottomThumb:MoveThumbComponent;

        private var mAssistLine:EditAssistLineComponent;
        private var mScaleImgSource:EditScaleImgSourceComponent;

        private var mUpdateTextureBitmapCallBack:Function;

        private var mTopLeft:BitmapData;
        private var mTopRight:BitmapData;
        private var mBottomLeft:BitmapData;
        private var mBottomRight:BitmapData;
        private var mMiddle:BitmapData;
        private var mTopMiddle:BitmapData;
        private var mLeftMiddle:BitmapData;
        private var mRightMiddle:BitmapData;
        private var mBottomMiddle:BitmapData;

        public function EditViewport()
        {
        }

        public function initialize(_guiMc:MovieClip, _updateTextureBitmapCallBack:Function):void
        {
            mGUIMc = _guiMc;
            mUpdateTextureBitmapCallBack = _updateTextureBitmapCallBack;
            mEditAreaMc = mGUIMc["editAreaMc"];

            mHorizontalLeftThumb = new MoveThumbComponent();
            mHorizontalLeftThumb.initialize(mEditAreaMc["horizontalLeftThumb"],
                                            MoveThumbComponent.THUMB_TYPE_HORIZONTAL,
                                            onThumbMove);

            mHorizontalRightThumb = new MoveThumbComponent();
            mHorizontalRightThumb.initialize(mEditAreaMc["horizontalRightThumb"],
                                             MoveThumbComponent.THUMB_TYPE_HORIZONTAL,
                                             onThumbMove);

            mVerticalTopThumb = new MoveThumbComponent();
            mVerticalTopThumb.initialize(mEditAreaMc["verticalTopThumb"],
                                         MoveThumbComponent.THUMB_TYPE_VERTICAL,
                                         onThumbMove);

            mVerticalBottomThumb = new MoveThumbComponent();
            mVerticalBottomThumb.initialize(mEditAreaMc["verticalBottomThumb"],
                                            MoveThumbComponent.THUMB_TYPE_VERTICAL,
                                            onThumbMove);

            mAssistLine = new EditAssistLineComponent();
            mAssistLine.initialize(mEditAreaMc["editAreaMc"]["assistLinePH"]);

            mScaleImgSource = new EditScaleImgSourceComponent();
            mScaleImgSource.initialize(mEditAreaMc["editAreaMc"]["scaleImgSourcePH"], onSourceDropIn);

            onThumbMove();
        }

        private function onSourceDropIn():void
        {
            onThumbMove();
            updateBitmapTexture();
        }

        private function onThumbMove():void
        {
            if (mScaleImgSource.isHaveImg())
            {
                mAssistLine.refresh(
                        mScaleImgSource.getImgBounds(),
                        mHorizontalLeftThumb.getValue(),
                        mHorizontalRightThumb.getValue(),
                        mVerticalTopThumb.getValue(),
                        mVerticalBottomThumb.getValue()
                );
                updateBitmapTexture();
            }

        }

        private function updateBitmapTexture():void
        {
            var imgBounds:Rectangle = mScaleImgSource.getImgBounds();
            var imgBitmapData:BitmapData = mScaleImgSource.getTargetImg().bitmapData;

            var calculateMatrix:Matrix = new Matrix();

            calculateMatrix.tx = 0;
            calculateMatrix.ty = 0;
            mTopLeft && mTopLeft.dispose();
            mTopLeft = doDrawBitmapTexture(mHorizontalLeftThumb.getValue() - imgBounds.left,
                                           mVerticalTopThumb.getValue() - imgBounds.top,
                                           calculateMatrix,
                                           imgBitmapData);


            calculateMatrix.tx = -mHorizontalRightThumb.getValue() + imgBounds.left;
            calculateMatrix.ty = 0;
            mTopRight && mTopRight.dispose();
            mTopRight = doDrawBitmapTexture(imgBounds.right - mHorizontalRightThumb.getValue(),
                                            mVerticalTopThumb.getValue() - imgBounds.top,
                                            calculateMatrix,
                                            imgBitmapData);


            calculateMatrix.tx = 0;
            calculateMatrix.ty = -mVerticalBottomThumb.getValue() + imgBounds.top;
            mBottomLeft && mBottomLeft.dispose();
            mBottomLeft = doDrawBitmapTexture(mHorizontalLeftThumb.getValue() - imgBounds.left,
                                              imgBounds.bottom - mVerticalBottomThumb.getValue(),
                                              calculateMatrix,
                                              imgBitmapData);

            calculateMatrix.tx = -mHorizontalRightThumb.getValue() + imgBounds.left;
            calculateMatrix.ty = -mVerticalBottomThumb.getValue() + imgBounds.top;
            mBottomRight && mBottomRight.dispose();
            mBottomRight = doDrawBitmapTexture(imgBounds.right - mHorizontalRightThumb.getValue(),
                                               imgBounds.bottom - mVerticalBottomThumb.getValue(),
                                               calculateMatrix,
                                               imgBitmapData);


            calculateMatrix.tx = -mHorizontalLeftThumb.getValue() + imgBounds.left;
            calculateMatrix.ty = 0;
            mTopMiddle && mTopMiddle.dispose();
            mTopMiddle = doDrawBitmapTexture(10,
                                             mVerticalTopThumb.getValue() - imgBounds.top,
                                             calculateMatrix,
                                             imgBitmapData);

            calculateMatrix.tx = 0;
            calculateMatrix.ty = -mVerticalTopThumb.getValue() + imgBounds.top;
            mLeftMiddle && mLeftMiddle.dispose();
            mLeftMiddle = doDrawBitmapTexture(mHorizontalLeftThumb.getValue() - imgBounds.left,
                                              10,
                                              calculateMatrix,
                                              imgBitmapData);


            calculateMatrix.tx = -mHorizontalRightThumb.getValue() + imgBounds.left;
            calculateMatrix.ty = -mVerticalTopThumb.getValue() + imgBounds.top;
            mRightMiddle && mRightMiddle.dispose();
            mRightMiddle = doDrawBitmapTexture(imgBounds.right - mHorizontalRightThumb.getValue(),
                                               10,
                                               calculateMatrix,
                                               imgBitmapData);


            calculateMatrix.tx = -mHorizontalLeftThumb.getValue() + imgBounds.left;
            calculateMatrix.ty = -mVerticalBottomThumb.getValue() + imgBounds.top;
            mBottomMiddle && mBottomMiddle.dispose();
            mBottomMiddle = doDrawBitmapTexture(10,
                                                imgBounds.bottom - mVerticalBottomThumb.getValue(),
                                                calculateMatrix,
                                                imgBitmapData);


            calculateMatrix.tx = -mHorizontalLeftThumb.getValue() + imgBounds.left;
            calculateMatrix.ty = -mVerticalTopThumb.getValue() + imgBounds.top;
            mMiddle && mMiddle.dispose();
            mMiddle = doDrawBitmapTexture(10,
                                          10,
                                          calculateMatrix,
                                          imgBitmapData);


            mUpdateTextureBitmapCallBack(topLeft, topRight, bottomLeft, bottomRight,
                                         middle,
                                         topMiddle, leftMiddle, rightMiddle, bottomMiddle);
        }

        private function doDrawBitmapTexture(_width:int, _height:int, _matrix:Matrix, _source:BitmapData):BitmapData
        {
            _width = Math.max(1, _width);
            _height = Math.max(1, _height);
            var resultBitmap:BitmapData = new BitmapData(_width, _height, true, 0);
            resultBitmap.draw(_source, _matrix);
            return resultBitmap;
        }


        public function get topLeft():BitmapData {return mTopLeft;}

        public function get topRight():BitmapData {return mTopRight;}

        public function get bottomLeft():BitmapData {return mBottomLeft;}

        public function get bottomRight():BitmapData {return mBottomRight;}

        public function get middle():BitmapData {return mMiddle;}

        public function get topMiddle():BitmapData {return mTopMiddle;}

        public function get leftMiddle():BitmapData {return mLeftMiddle;}

        public function get rightMiddle():BitmapData {return mRightMiddle;}

        public function get bottomMiddle():BitmapData {return mBottomMiddle;}
    }
}
